package cn.blue.pet.controller;
import cn.blue.base.config.BussinessException;
import cn.blue.base.util.LoginContext;
import cn.blue.org.domain.Employee;
import cn.blue.org.service.EmployeeService;
import cn.blue.pet.dto.AcceptDto;
import cn.blue.pet.service.ISearchMasterMsgService;
import cn.blue.pet.domain.SearchMasterMsg;
import cn.blue.pet.query.SearchMasterMsgQuery;
import cn.blue.base.util.AjaxResult;
import cn.blue.base.util.PageList;
import cn.blue.user.domain.Logininfo;
import cn.blue.user.domain.User;
import cn.blue.user.mapper.UserMapper;
import cn.blue.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IUserService userService;
    @Autowired
    private EmployeeService employeeService;


    /**
     * 保存和修改公用的
     *
     * @param searchMasterMsg 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id") Long id) {
        return searchMasterMsgService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<SearchMasterMsg> list() {

        return searchMasterMsgService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     * @param searchMasterMsg 发布寻主消息
     * @return
     */
    @PostMapping("/publish")
    AjaxResult search(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        //获取当前登陆人的信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //查询当前登陆人的用户信息
        User user = userService.findbyLogininfo(logininfo.getId());
        searchMasterMsg.setUserId(user.getId());
        searchMasterMsgService.search(searchMasterMsg);
        return AjaxResult.me().success();
    }

    /**
     * 待处理
     * state=1 shopId为当前登陆人的店铺id
     *
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/toHandle")
    PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery searchMasterMsg, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectBylogininfo(logininfo.getId());
        searchMasterMsg.setShopId(employee.getShopId());
        searchMasterMsg.setState(1);
        return searchMasterMsgService.selectSearchByParam(searchMasterMsg);

    }

    /**
     * 寻主消息审核失败 待发布
     * state=0
     *
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/toAudit")
    PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery searchMasterMsg, HttpServletRequest request) {
        searchMasterMsg.setState(0);
        return searchMasterMsgService.selectSearchByParam(searchMasterMsg);

    }

    /**
     * 寻主池
     * state=1 shopId=null
     *
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/pool")
    PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery searchMasterMsg, HttpServletRequest request) {
        searchMasterMsg.setState(1);
        return searchMasterMsgService.selectSearchByParam(searchMasterMsg);

    }

    /**
     * 查询已完成寻主消息
     * state=3 shopId=null 平台管理员shopId=null 店铺管理员:shopId等于当前登陆人shopId
     *
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/finish")
    PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery searchMasterMsg, HttpServletRequest request) {
        searchMasterMsg.setState(3);
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectBylogininfo(logininfo.getId());
        //shopId等于null是平台管理员 不为null就需要设置shopid
        if (employee.getShopId() != null) {
            searchMasterMsg.setShopId(employee.getShopId());
        }
        return searchMasterMsgService.selectSearchByParam(searchMasterMsg);

    }

    /**
     * user
     * state=3 s
     *
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/user")
    PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery searchMasterMsg, HttpServletRequest request) {

        return searchMasterMsgService.selectSearchByParam(searchMasterMsg);

    }

    /**
     * 拒单
     *
     * @param
     * @param request
     * @return
     */
    @PostMapping("/reject/{id}")
    AjaxResult reject(@PathVariable("id") Long id, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectBylogininfo(logininfo.getId());
        searchMasterMsgService.reject(id, employee);
        return AjaxResult.me().success();

    }

    /**
     * 接单
     */
    @PostMapping("/accept")
    AjaxResult accept(@RequestBody AcceptDto dto)throws Exception {
        searchMasterMsgService.accept(dto);


        return AjaxResult.me().success();


    }
}